package com.zxx.coolweather._wuguan2;

import android.content.Context;

import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.rest.OnResponseListener;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.RequestQueue;
import com.yanzhenjie.nohttp.rest.Response;

/**
 * 异步请求的步骤
 * 1.创建队列
 * @author zxx on 2022/1/12
 */
public class CallServer {

    private static CallServer instance;
    private RequestQueue queue;

    public synchronized static CallServer getInstance(){
        if (instance == null) {
            instance = new CallServer();
        }
        return instance;
    }

    private CallServer() {
        queue = NoHttp.newRequestQueue();   //默认并发值为3 或者传入并发值
    }

    public <T> void add(Context context, Request<T> request, HttpCallBack<T> callBack, int what, boolean isShowDialog, boolean isCanCancel, boolean isShowError) {
        queue.add(what, request, new ResponseListener<T>(request, context, callBack, isShowDialog, isCanCancel, isShowError));
    }

    /**
     * 取消这个sign标记的所有请求。
     * @param sign 请求的取消标志。
     */
    public void cancelBySign(Object sign) {
        queue.cancelBySign(sign);
    }

    /**
     * 取消队列中所有请求。
     */
    public void cancelAll() {
        queue.cancelAll();
    }

}
